package com.zl.org.controller;

import com.zl.basic.annotation.PreAuthorize;
import com.zl.basic.util.AjaxResult;
import com.zl.basic.util.PageList;
import com.zl.org.domain.Employee;
import com.zl.org.query.EMPQuery;
import com.zl.org.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpService empService;
    /**
     * 查询一个对象的数据
     * */
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个对象",sn = "emp:getOne")
    public Employee findOne(@PathVariable("id") Long id) {
        return empService.findById(id);
    }
    /**
     * 查询所有
     * */
    @GetMapping
    public List<Employee> findAll() {
        return empService.findAll();
    }
    /**
     * 分页查询
     * */
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EMPQuery empQuery) {

        return empService.queryPage(empQuery);
    }

    /**
     * 删除操作
     * */
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            empService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！！！");
        }
    }

    /**
     * 添加或者修改操作
     * */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody  Employee employee) {
        try {
            if (employee.getId()==null){
                empService.save(employee);
            }else {
                empService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！！！");
        }
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            empService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！！！");
        }
    }

    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId) {

        return empService.list(shopId);
    }
}
